/**
 * 占位符填充
 * @param {string} value 占位符字符串
 * @param {stirng | object} values 占位符参数
 * 示例：
 * var str  = wx.$g.placeholder("js实现用{two}自符串替换占位符{two} {three}  {one} ").format({one: "I", two: "LOVE", three: "YOU"});
 * var str2 = wx.$g.placeholder("js实现用{1}自符串替换占位符{1} {2}  {0} ").format("I", "LOVE", "YOU");
 */
// 单例
let SingletonPlaceholder;

// 主要对象
function Placeholder() {
	// 参数
	let value = '';
	// 初始化
	this.init = function(param) {
		value = param;
		return this;
	}
	// 占位符
	this.format = function() {
		var param = arguments[0];
		var s = value;
		if (typeof(param) == 'object') {
			for (var key in param)
				s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
			return s;
		} else {
			for (var i = 0; i < arguments.length; i++)
				s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
			return s;
		}
	}
}

// 封装初始化
let placeholder = function(value) {
	if (value == null || value.length == 0) return value;

	if (SingletonPlaceholder == null) {
		SingletonPlaceholder = new Placeholder();
	}
	return SingletonPlaceholder.init(value);
}

 export default placeholder;